package org.bleep.scene;

import java.util.Hashtable;

public class Node
{
	protected Node[] children;
	protected int numChildren;
	protected Hashtable names;

	public Node()
	{
		children = new Node[10];
		numChildren = 0;
		names = new Hashtable();
	}

	public void pre()
	{
		// nothing
	}

	public void post()
	{
		// nothing
	}

	public void add(String name, Node child)
	{
		ensureCapacity();
		children[numChildren] = child;
		names.put("name", new Integer(numChildren));
		numChildren++;
	}
	
	public void delete(String name)
	{
		children[((Integer)names.get(name)).intValue()] = null;
		names.remove(name);
	}
	
	public Node get(String name)
	{
		return children[((Integer)names.get(name)).intValue()];
	}

	public void traverse()
	{
		pre();
		int currentChild = 0;
		while (currentChild < numChildren && children != null)
		{
			if (children[currentChild] != null)
			{
				children[currentChild].traverse();
			}
			currentChild++;
		}
		post();
	}

	public void ensureCapacity()
	{
		if (numChildren == children.length)
		{
			Node[] temp = new Node[numChildren + numChildren / 2];
			System.arraycopy(children, 0, temp, 0, children.length);
			children = temp;
		}		
	}
}